package com.manyiaby.view.viewutils;

import android.os.Environment;
import android.os.StatFs;
import android.text.format.Formatter;

import com.manyiaby.welcome.ProxyApplication;

import java.io.File;

/**
 * Created by yx on 2017/10/13.
 * 内存计算和判断
 */

public class MemoryUtil {

    /**
     * 获得SD卡总大小
     * 在API level 18中已经使用getBlockSizeLong () 来代替getBlockSize ()这个方法了。
     * 但是需要注意的是，getBlockSizeLong ()这个方法在API level 18版本之前是没有的，如果开发的应用需要兼容18版本以前的系统，最好还是使用getBlockSize()方法。
     *
     * @return
     */
    public static String getSDTotalSize() {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();//过时，看上方注解解释↑
        long totalBlocks = stat.getBlockCount();
        return Formatter.formatFileSize(ProxyApplication.getInstance(), blockSize * totalBlocks);
    }

    /**
     * 获得sd卡剩余容量，即可用大小
     *
     * @return
     */
    public static String getSDAvailableSize() {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return Formatter.formatFileSize(ProxyApplication.getInstance(), blockSize * availableBlocks);
    }

    /**
     * 获得机身内存总大小
     *
     * @return
     */
    public static String getRomTotalSize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return Formatter.formatFileSize(ProxyApplication.getInstance(), blockSize * totalBlocks);
    }

    /**
     * 获得机身可用内存
     *
     * @return
     */
    public static String getRomAvailableSize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return Formatter.formatFileSize(ProxyApplication.getInstance(), blockSize * availableBlocks);
    }


    /**
     * 判断是否有SD卡
     *
     * @return
     */
    public static String getSDPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState()
                .equals(Environment.MEDIA_MOUNTED); //判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();//获取跟目录
        }
        return sdDir.toString();
    }

}
